<?php
/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */

App::uses('Controller', 'Controller');

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package		app.Controller
 * @link		http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
 */
class AppController extends Controller {
	public $components = array('Session','RequestHandler', /*'DebugKit.Toolbar',*/ 'Authake.Authake');
	public $helpers = array('Js', 'Form', 'Time', 'Html', 'Session',  'SideBar.SideBar', 'Authake.Authake');

	public $SideBarItems = '';
	public $SideBarMessage = '';
	public $HMenu = '/';
	public $VMenu = '';
	
	public $SessionUserData = '';

	function beforeFilter(){
		$this->set('HMenu', $this->HMenu);
		$this->auth();

		Configure::write('Config.language', 'rus');
	}	
	function afterFilter() {
		if ($this->Session->check('LogsSettings')) {
			// подключаем модель логов
			$this->loadModel('Userlog');
			// выбираем из сессии учетные данные польхователя - login и группы в которых он находится
			$logsettings = explode('|', $this->Session->read('LogsSettings'));
			// берем url который он запрашивает
			$log_url = $this->request->url;
			$newLog = array('Userlog' => array('login' => $logsettings[0],'groups' => $logsettings[1],  'url' => $log_url));
			//debug($newLog);
			//  пишем в таблицу логин группы действие пользователя и время совершения действия
			$this->Userlog->create();
			$this->Userlog->save($newLog);
			// подключаем модель OnlineUser
			$this->loadModel('Onlineuser');
			$login = $logsettings[0];
			$nowtime = date('Y-m-d H:i:s');
			// обновляем lastaction время последнего действия пользователя
			$onlineuser_id = $this->Onlineuser->find('all', array('conditions' =>
					array('Onlineuser.login' => $login), 'fields' => array('Onlineuser.id')));
			if ($onlineuser_id){
				$this->Onlineuser->id = $onlineuser_id[0]['Onlineuser']['id'];
				$this->Onlineuser->savefield('lastaction', $nowtime);
			}
			// формируем время инактивности
			$year = date('Y'); $month = date('m'); $day = date('d'); $hour = date('H'); $minutes = date('i'); $seconds = date('s');
			if (($minutes - 10)<0){
				if (($hour - 1)<0){
					$hour = 23;
				} else {
					$hour -= 1;
				}
				$minutes = 60 - (10 - $minutes);
			} else {
				if ($minutes <20){
					$minutes = '0'.($minutes-10);
				} else {
					$minutes -= 10;
				}
			}
			$cleartime = $year.'-'.$month.'-'.$day.' '.$hour.':'.$minutes.':'.$seconds;
			// удаляем тех пользователей последнее действие которых было более 10 минут назад
			$this->Onlineuser->deleteall(array('Onlineuser.lastaction <' => $cleartime), false);
			
			//echo Configure::version();
		}
	}
	
	private function auth(){
		Configure::write('Authake.useDefaultLayout', true);
		$this->Authake->beforeFilter($this);
	}
}
